//
//  DemandListViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/1.
//

import UIKit
import CollectionKit
import JXSegmentedView
import RxCocoa
import RxSwift

class DemandListViewController: UIViewController {

  private var collectionView = CollectionView()
  private let disposeBag = DisposeBag()

  var type = DemandStatus.unpaidFee

  override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor(hex: 0xFAFAFA)
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { make in
      if type == .unpaidFee {
        let inset = UIEdgeInsets(top: 0, left: 0, bottom: 88, right: 0)
        make.edges.equalToSuperview()
          .inset(inset)
      } else {
        make.top.right.left.equalToSuperview()
        make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
      }
    }
    
    let dataSource = ArrayDataSource<RepairOrderModel>(data:[
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
      RepairOrderModel(),
    ])

    let viewSource = ClosureViewSource<RepairOrderModel, DemandItemContentView> { model, index in
      return R.nib.demandItemContentView(owner: self)!
    } viewUpdater: { item, model, index in
      item.status = self.type
    }

    let sizeSource = { (index: Int, data: RepairOrderModel, size: CGSize) in
      return CGSize(width: size.width - 30, height: 150)
    }

    let inset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)

    let itemProvider = BasicProvider<RepairOrderModel, DemandItemContentView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 8, justifyContent: .center)
        .inset(by: inset)
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jumpToDetail(index: context.index)
    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])

    if type == .unpaidFee {
      let bottomView = R.nib.bottomButtonContentView(owner: self)!
      bottomView.button.setTitle("一键催费", for: .normal)
      view.addSubview(bottomView)
      bottomView.snp.makeConstraints { make in
        make.right.left.bottom.equalToSuperview()
        make.height.equalTo(88)
      }
      bottomView.button.rx.tap
        .subscribe(onNext: {
          self.jumpToPaid()
        })
        .disposed(by: disposeBag)
    }
  }

  func jumpToDetail(index: Int) {

    let vc = DemandDetailViewController()
    vc.type = type
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }

  func jumpToPaid() {

//    let vc = DemandDetailViewController()
//    vc.hidesBottomBarWhenPushed = true
//    navigationController?.pushViewController(vc, animated: true)

  }
  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

extension DemandListViewController: JXSegmentedListContainerViewListDelegate {

  func listView() -> UIView {
    return self.view
  }
}
